﻿using PacoToolKit.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace OUS_HUB
{
    static class Program
    {
        /// <summary>
        /// 是否退出应用程序
        /// </summary>
        static bool glExitApp = false;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Logger.LoadSimpleHourRollingFileAppender();
            //处理未捕获的异常
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Application.Run(new FormMain());
            glExitApp = true;//标志应用程序可以退出
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //LogHelper.Save("CurrentDomain_UnhandledException", LogType.Error);
            Logger.Error("CurrentDomain_UnhandledException");
            //LogHelper.Save("IsTerminating : " + e.IsTerminating.ToString(), LogType.Error);
            Logger.Error("IsTerminating : " + e.IsTerminating.ToString());
            //LogHelper.Save(e.ExceptionObject.ToString());
            Logger.Error(e.ExceptionObject.ToString());

            while (true)
            {
                //循环处理，否则应用程序将会退出
                if (glExitApp)
                {
                    //标志应用程序可以退出，否则程序退出后，进程仍然在运行
                    Logger.Info("ExitApp");

                    CmdStartCTIProc(Application.ExecutablePath, "cmd params");
                    return;
                }
                System.Threading.Thread.Sleep(2 * 1000);
            };
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Logger.Error("Application_ThreadException:" + e.Exception.Message);
            Logger.Error(e.Exception);
        }

        /// <summary>
        /// 在命令行窗口中执行
        /// </summary>
        /// <param name="sExePath"></param>
        /// <param name="sArguments"></param>
        static void CmdStartCTIProc(string sExePath, string sArguments)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = false;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            p.StandardInput.WriteLine(sExePath + " " + sArguments);
            p.StandardInput.WriteLine("exit");
            p.Close();

            System.Threading.Thread.Sleep(9000);//必须等待，否则重启的程序还未启动完成；根据情况调整等待时间
        }
    }
}
